package com.sunny.ihealth.data;

import com.sunny.ihealth.entity.app.SportData;
import com.sunny.ihealth.entity.app.UserConfig;
import com.sunny.ihealth.utils.TimeUtils;

import org.litepal.crud.DataSupport;

import java.util.List;

/**
 * 本地数据支持
 * Created by Sunny  An on 2016/6/5.
 */
public class LocalData {

    private static String getUsername(String username) {
        if (username == null) {
            username = UserConfig.VISITOR;
        }
        return username;
    }

    private static UserConfig getDefaultConfig(String username) {
        UserConfig config = new UserConfig();
        config.setUsername(getUsername(username));
        config.setWalk(2000);
        config.setRunType(UserConfig.TYPE_DISTANCE);
        config.setRunDistance(1000);
        config.setRunTime(20);
        config.setSleepTime(7 * 60);
        return config;
    }

    /**
     * 获取用户的配置文件
     */
    public static UserConfig getUserConfig(String username) {
        username = getUsername(username);
        List<UserConfig> configs = DataSupport.where("username=?", username).limit(1).find(UserConfig.class);
        if (configs.size() == 0) {
            //没找到
            UserConfig config = getDefaultConfig(username);
            config.save();
            return config;
        } else {
            //找到了
            return configs.get(0);
        }
    }

    public static boolean setUserConfig(UserConfig config) {
        return config.save();
    }


    private static SportData getDefaultSportData(String username) {
        username = getUsername(username);
        SportData sportData = new SportData();
        sportData.setUsername(username);
        sportData.setRunTime(0);
        sportData.setSleepTime(0);
        sportData.setRunDistance(0);
        sportData.setWalk(0);
        sportData.setDay(TimeUtils.getCreateTime(0));
        return sportData;
    }

    public static SportData getTodaySportData(String username) {
        username = getUsername(username);
        long day = TimeUtils.getCreateTime(0);
        List<SportData> data = DataSupport.where("username=? and day=?", username, day + "").limit(1).find(SportData.class);
        if (data.size() == 0) {
            SportData sportData = getDefaultSportData(username);
            sportData.save();
            return sportData;
        } else {
            return data.get(0);
        }
    }

    public static boolean saveWalkStep(String username, int walkStep) {
        username = getUsername(username);
        long day = TimeUtils.getCreateTime(0);
        SportData sportData = getTodaySportData(username);
        sportData.setWalk(sportData.getWalk() + walkStep);
        return sportData.save();
    }

    public static boolean saveDistanceAndTime(String username, int distance, int time) {
        username = getUsername(username);
        long day = TimeUtils.getCreateTime(0);
        SportData sportData = getTodaySportData(username);
        sportData.setRunDistance(sportData.getRunDistance() + distance);
        sportData.setRunTime(sportData.getRunTime() + time);
        return sportData.save();
    }

    public static boolean saveSleepTime(String username, int time) {
        username = getUsername(username);
        long day = TimeUtils.getCreateTime(0);
        SportData sportData = getTodaySportData(username);
        sportData.setSleepTime(time);
        return sportData.save();
    }


}
